var app = getApp();
// 引入SDK核心类
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');

var qqmapsdk;
// 城市文件
var city = require('../../utils/city.js')
var lineHeight = 0;
var endWords = "";
var isNum;
Page({
  data: {
    searchIsValue:false,
    "hidden": true,
    cityName: "", //获取选中的城市名
    hotel: [],
    address: '',
    searchAddress: '',
    toView: 'inToView0',
    tags: [
      {
        name: '北京',
        id: 0
      }, {
        name: '上海',
        id: 1
      }, {
        name: '成都',
        id: 2
      },
      {
        name: '重庆',
        id: 3
      }, {
        name: '杭州',
        id: 4
      },
      {
        name: '西安',
        id: 5
      },
      {
        name: '南京',
        id: 6
      },
      {
        name: '武汉',
        id: 7
      },
      {
        name: '厦门',
        id: 8
      }
    ],
    // 0从首页来，1从列表页进来
    whereFrom:'0'

  },
  tagChoose: function (e) {
    var that = this;
    console.log(that.data.whereFrom);
    if (that.data.whereFrom == 0) {
      wx.redirectTo({
        url: 'index_c?cityName=' + e.currentTarget.dataset.address
      })
    } else if (that.data.whereFrom == 1){
      wx.redirectTo({
        url: '../hotel_list/hotel_list?keywords=' + e.currentTarget.dataset.address
      })
    }
  },
  scrollToViewFn: function (e) {
    var _id = e.currentTarget.dataset.id;
    console.log(e);
    this.setData({
      toView: 'inToView' + _id
    })
    console.log(this.data.toView)
  },
  onLoad: function(o) {
    console.log(o);
    var that = this;
   
    // 生命周期函数--监听页面初次渲染完成
    var cityChild = city.City[0];
    wx.getSystemInfo({
      success: function (res) {
        lineHeight = (res.windowHeight - 100) / 22;
        console.log(res.windowHeight - 100)
        that.setData({
          city: cityChild,
          winHeight: res.windowHeight - 40,
          lineHeight: lineHeight
        })
      }
    })
    // 
    that.setData({
      searchAddress: o.address,
      whereFrom: o.whereFrom
    });
    console.log(o.address);
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#3090F4'
    });
    app.util.request({
      url: "entry/wxapp/JdList",
      cachetime: "0",
      data: {
        keywords: o.address
      },
      success: function (o) {
        console.log(o);
        that.setData({
          hotel: o.data
        });
      },
      fail: function () {
        that.setData({
          hotel: []
        })
      }
    });
    // 实例化API核心类JB4BZ-DH2CP-6GEDQ-VMHYX-ON2P7-WYBSB
    qqmapsdk = new QQMapWX({
      key: 'DXJBZ-ZQEKW-UOPRE-RAZT3-CI6NK-WOBF5'
    });
    //1、获取当前位置坐标
    wx.getLocation({
      type: 'wgs84',
      success: function(res) {
        console.log(res);
        //2、根据坐标获取当前位置名称，显示在顶部:腾讯地图逆地址解析
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function(addressRes) {
            console.log(addressRes);
            var address = addressRes.result.formatted_addresses.recommend;
            that.setData({
              address: address,
              latitude: res.latitude,
              longitude: res.longitude
            })
          },
          fail: function(e) {
            console.log(e);
          },
          complete: function(e) {
            console.log(e);
          }
        })
      }
    })
  },
  search_hotel: function(o) {
    console.log(o);
    this.setData({
      searchIsValue: o.detail.value !=""? true : false
    })
    var t = this,    
      e = o.detail.value;
    "" != e ? app.util.request({
      url: "entry/wxapp/JdList",
      cachetime: "0",
      data: {
        keywords: e
      },
      success: function(o) {
        console.log(o), t.setData({
          hotel: o.data,
          requestResult: o.data == ''? true:false
        });
        
      }
    }) : t.setData({
      hotel: []
    });
  },
  local: function() {
    var a = this;
    var t = a.data;
    wx.setStorageSync("latitude", t.latitude), wx.setStorageSync("longitude", t.longitude),
      wx.navigateTo({
        url: "../hotel_list/hotel_list?nearby=0"
      });
  },
  search_result: function(o) {
    var t = o.currentTarget.dataset.id;
    if(this.data.whereFrom == 0){
      wx.redirectTo({
        url: 'index_c?cityName=' + o.currentTarget.dataset.name
      })
    } else if (this.data.whereFrom == 1){
      wx.redirectTo({
        url: '../hotel_list/hotel_list?keywords=' + o.currentTarget.dataset.name
      })
    }
    
    // wx.navigateTo({
    //   url: "../hotel_list/hotel_info?hotel_id=" + t
    // });
  },
  //触发全部开始选择
  chStart: function () {
    this.setData({
      trans: ".3",
      hidden: false
    })
  },
  //触发结束选择
  chEnd: function () {
    this.setData({
      trans: "0",
      hidden: true,
      scrollTopId: this.endWords
    })
  },
  //获取文字信息
  getWords: function (e) {
    var id = e.target.id;
    this.endWords = id;
    isNum = id;
    this.setData({
      showwords: this.endWords
    })
    console.log(this.endWords);
  },
  //设置文字信息
  setWords: function (e) {
    var id = e.target.id;
    this.setData({
      scrollTopId: id
    })
  },

  // 滑动选择城市
  chMove: function (e) {
    var y = e.touches[0].clientY;
    var offsettop = e.currentTarget.offsetTop;
    var height = 0;
    var that = this;
    ;
    var cityarr = ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z"]
    // 获取y轴最大值
    wx.getSystemInfo({
      success: function (res) {
        height = res.windowHeight - 10;
      }
    });

    //判断选择区域,只有在选择区才会生效
    if (y > offsettop && y < height) {
      // console.log((y-offsettop)/lineHeight)
      var num = parseInt((y - offsettop) / lineHeight);
      endWords = cityarr[num];
      // 这里 把endWords 绑定到this 上，是为了手指离开事件获取值
      that.endWords = endWords;
    };


    //去除重复，为了防止每次移动都赋值 ,这里限制值有变化后才会有赋值操作，
    //DOTO 这里暂时还有问题，还是比较卡，待优化
    if (isNum != num) {
      // console.log(isNum);
      isNum = num;
      that.setData({
        showwords: that.endWords
      })
    }
  },
  //选择城市，并让选中的值显示在文本框里
  bindCity: function (e) {
    console.log(e.currentTarget.dataset.city);
    var cityName = e.currentTarget.dataset.city;
    this.setData({ cityName: cityName });
    if (this.data.whereFrom == 0){
      wx.redirectTo({
        url: 'index_c?cityName=' + cityName
      })
    } else if (this.data.whereFrom == 1){
      wx.redirectTo({
        url: '../hotel_list/hotel_list?keywords=' + cityName
      })
    }
  },
  onReady: function() {},
  onShow: function() {},
  onHide: function() {
    this.setData({
      hotel: []
    });
  },
  onUnload: function() {},
  onPullDownRefresh: function() {},
  onReachBottom: function() {}
});